package com.zhiwang.planet.ui.my

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import com.zhiwang.planet.BuildConfig
import com.zhiwang.planet.R
import com.zhiwang.planet.databinding.ActivityAccountSettingBinding
import com.zhiwang.planet.di.component.DaggerLoginComponent
import com.zhiwang.planet.di.module.AccountModule
import com.zhiwang.planet.di.module.AutoDisposeModule
import com.zhiwang.planet.event.LogoutEvent
import com.zhiwang.planet.event.PhoneBindEvent
import com.zhiwang.planet.event.TabChangeEvent
import com.zhiwang.planet.mvp.p.CheckUpdatePresenter
import com.zhiwang.planet.mvp.p.LoginLogoutPresenter
import com.zhiwang.planet.mvp.p.UserPresenter
import com.zhiwang.planet.mvp.v.*
import com.zhiwang.planet.mvvm.model.entity.UserDao
import com.zhiwang.planet.mvvm.model.entity.main.UpdateCheck
import com.zhiwang.planet.mvvm.viewmodel.my.AccountViewModel
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.ui.LoginActivity
import com.zhiwang.planet.ui.PrivacyActivity
import com.zhiwang.planet.ui.UpdateActivity
import com.zhiwang.planet.util.*
import com.zhiwang.planet.widget.SimpleDialogBuilder
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.activity_account_setting.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import javax.inject.Inject

class AccountSettingActivity : BaseActivity(), ApkUpdateView, ApkUpdateView2, LogoutView {


    val TAG = "AccountSettingActivity"
    override fun onUpdate2(updateCheck: UpdateCheck) {
        if (updateCheck.version > BuildConfig.VERSION_CODE) {
            version_update_indicator.setVisible()
        } else version_update_indicator.setGone()
    }

    override fun onUpdate(updateCheck: UpdateCheck) {

        if (updateCheck.version > BuildConfig.VERSION_CODE) {
            UpdateActivity.start(this, updateCheck)
            overridePendingTransition(R.anim.anim_fade_in, R.anim.anim_fade_out)
        } else {
            Toaster.message("已经是最新版了")
        }
    }

    @Inject
    lateinit var loginPresenter: LoginLogoutPresenter

    @Inject
    lateinit var userPresenter: UserPresenter

    @Inject
    lateinit var updatePresenter: CheckUpdatePresenter

    @Inject
    lateinit var userDao: UserDao

    @Inject
    lateinit var accountViewModel: AccountViewModel

    var phoneBind = false

    override fun initInjection() {
        super.initInjection()

        DaggerLoginComponent.builder()
                .accountModule(AccountModule(this))
                .appComponent(getAppComponent())
                .autoDisposeModule(AutoDisposeModule(this))
                .name("Logout")
                .build()
                .inject(this)
        updatePresenter.apkUpdateView = this
        loginPresenter.logoutView = this
    }

    var disposable: Disposable? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding =
                DataBindingUtil.setContentView<ActivityAccountSettingBinding>(this, R.layout.activity_account_setting)
        binding.viewModel = accountViewModel
        binding.lifecycleOwner = this
        setSupportActionBar(toolbar)
        update_layout.setOnClickListener {
            updatePresenter.checkUpdate()
        }

        accountViewModel.err.observe(this, Observer {
            onError(it)
        })

        accountViewModel.hasLogout.observe(this, Observer { loggedOut ->
            if (loggedOut) {
                AppUtil.handleLogout(this)
                suicideAfterTransition()
                EventBus.getDefault().post(LogoutEvent())
                EventBus.getDefault().post(TabChangeEvent(0))
            }
        })

//        accountViewModel.binded.observe(this, Observer {
//            phoneBind = it
//        })

        Utils.setStatusBar(this,false,false)

        Utils.setStatusTextColor(true, this)
    }



    fun tryModifyPassword(v: View) {
        if (phoneBind.not()) {
            showBindPhoneAlertDialog()
        } else {
            startActivity(Intent(this, ModifyPasswordActivity::class.java))
        }

    }

    private fun showBindPhoneAlertDialog() = SimpleDialogBuilder()
            .title(getString(R.string.hint))
            .titleTextColor(R.color.colorPrimary)
            .buttonText(getString(R.string.iknow))
            .content(getString(R.string.bind_phone_first))
            .onButtonClick(View.OnClickListener {
                quickStartActivity(BindPhoneActivity::class.java)
            })
            .build(this)
            .show()

    fun tryModifyPayPassword(v: View) {
        if (phoneBind.not())
            showBindPhoneAlertDialog()
        else quickStartActivity(BindPayPasswordActivity::class.java)
    }

    fun trySendFeedBack(v: View) {
        startActivity(Intent(this, FeedBack2Activity::class.java))
    }

    fun changeImei(v:View) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
            SimpleDialogBuilder()
                    .scaleAnim()
                    .content("当前使用的 Android 10 系统，无需切换设备码;如果您近期升级过 Android 系统，之前试玩过的游戏可能无法绑定")
                    .buttonText("我知道了")
                    .build(this)
                    .show()
        } else
            quickStartActivity(ChangeImeiActivity::class.java)
    }

    override fun onLogout() {
        onAuthExpired()
    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return super.onSupportNavigateUp()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onPhoneBind(e: PhoneBindEvent) {
        userPresenter.queryUserInfo()
    }

    fun changeNickName(view: View) {
        quickStartActivity(ChangeNickNameActivity::class.java)
    }

    fun changeAvatar(view: View) {
        quickStartActivity(ChangeAvatarActivity::class.java)
    }

    fun changeBindPhone(view: View) {
        quickStartActivity(ChangeBindedPhoneActivity::class.java)
    }

}
